/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package websitereader;

import java.io.*;
import java.net.URL;

public class WebsiteReader
{
        /*the url to be read from*/
        //public static String url = "http://finance.yahoo.com/q/ks?s=LLTC+Key+Statistics";
        
        /*location of file containing the all the ticker symbols*/
        //public static File tickers = new File("C:\\Users\\P7\\Downloads\\tickers.txt");
        
        /*use the following tags and extractKeyData() method to find the corresponding
         * data.
         */
        //v3m = average volume 3 month
        public final static String v3m = "Avg Vol (3 month)";
        
        public final static String v3m_tag = "tabledata1\">";
        
        //v10d = average volume 10days
        public final static String v10d = "Avg Vol (10 day)";
        
        public final static String v10d_tag = "tabledata1\">";
        
        public final static String roa = "Return on Assets (ttm):";

        public final static String roa_tag = "tabledata1\">";
        
        public final static String roe = "Return on Equity (ttm):";

        public final static String roe_tag = "tabledata1\">";
        
        public final static String price = "($dataItem, 'Last')\">";

        public final static String pe = "Trailing P/E (ttm, intraday):";

        public final static String pe_tag = "tabledata1\">";

        public final static String fpe = "Forward P/E";

        public final static String fpe_tag = "tabledata1\">";

        public final static String sector = "Sector:";

        public final static String sector_tag = "conameu.html\">";

        public final static String industry = "Industry:";

        public final static String industry_tag = "html\">";
        
        public final static String profit_margin = "Profit Margin (ttm):";
        
        public final static String pm_tag = "tabledata1\">";
        
        public final static String operating_margin = "Operating Margin (ttm):";
        
        public final static String om_tag = "tabledata1\">";
        
        public final static String[][] tags = {{v3m, v3m_tag}, {v10d, v10d_tag}};
        
        /******************************************************/
            
	public static BufferedReader read(String url) throws Exception{
		return new BufferedReader(
			new InputStreamReader(
				new URL(url).openStream())
                        );
        }
        
        /*reads the source code of the given url*/
        public static String readURL(String url) throws Exception{
            String content = "";
            /*try{
            BufferedReader reader = read(url);
            } catch (Exception e){
                return content;
            }*/
            BufferedReader reader = read(url);
            String line = reader.readLine();
            while(line!=null){
            content = content + line + "\n";
            line = reader.readLine();
            }
            return content;
        }
        
        /*prints the key statistics from the given url source code and returns 
          the subcontent
         */
        public static String extractKeyData(String content, String tag, String special_tag){
            int i = content.indexOf(tag);
            if (i == -1){
                return "";
            }
            int j = i + tag.length();
            String subcontent = content.substring(j, Math.min(j+1000, content.length()));
            i = subcontent.indexOf(special_tag);
            if (i == -1){
                return subcontent;
            }
            j = i + special_tag.length();
            subcontent = subcontent.substring(j, Math.min(j+500, subcontent.length()));
            String stat = subcontent.substring(0, subcontent.indexOf("<"));
            boolean is_stat = stat.substring(0, 1).equals("-") ||
                              stat.substring(0, 1).equals("0") ||  
                              stat.substring(0, 1).equals("1") ||
                              stat.substring(0, 1).equals("2") ||
                              stat.substring(0, 1).equals("3") ||
                              stat.substring(0, 1).equals("4") ||
                              stat.substring(0, 1).equals("5") ||
                              stat.substring(0, 1).equals("6") ||
                              stat.substring(0, 1).equals("7") ||
                              stat.substring(0, 1).equals("8") ||
                              stat.substring(0, 1).equals("9") ||
                              stat.substring(0, 1).equals("N");

            if (is_stat == true){
                System.out.print(stat + " ");
            }
            return subcontent;
        }
        
        /*reads the ticker symbols from the tickers file, extracts the key 
         * statistics from the corrsponding urls, and writes them to a text file
         */
        public static void loopReader(String tickersFile) throws Exception{
            BufferedReader tickerReader = new BufferedReader(new FileReader(new File(tickersFile)));
            String ticker = tickerReader.readLine();
            while(ticker!=null){
                String new_url = "http://finance.yahoo.com/q/ks?s=" + ticker;
                String content = readURL(new_url);
                System.out.print(ticker + " ");
                for(String[] tag : tags){
                    content = extractKeyData(content, tag[0], tag[1]);
                }
                System.out.print("\n");
                ticker = tickerReader.readLine();
            }
        }
         
	public static void main (String[] args) throws Exception{
            long startTime = System.currentTimeMillis();
            loopReader("C:\\Users\\P7\\Documents\\NetBeansProjects\\websiteReader\\tickers.txt");
            long endTime = System.currentTimeMillis();
            System.out.print(endTime - startTime);
}   
}
